<template>
  <div class="login">
    <!-- 自定义插槽 -->
    <van-nav-bar @click-left="toFind">
      <template #left>
        <i class="iconfont">&#xe62c;</i>
      </template>
    </van-nav-bar>
    <div class="box">
      <h2>账号密码登录</h2>
      <div class="input">
        <!-- 表单验证 -->
        <van-form ref="form">
          <div class="user">
            <van-icon name="manager" size="30" />
            <van-field
              placeholder="请输入用户名"
              v-model="form.userName"
              :rules="rules.userName"
            />
          </div>
          <div class="mima">
            <van-icon name="lock" size="30" />
            <van-field
              type="password"
              placeholder="请输入密码"
              v-model="form.password"
              :rules="rules.passWord"
            />
          </div>
        </van-form>
      </div>
      <div class="text">
        <span>手机号登录</span>
        <span>忘记密码</span>
      </div>
      <div class="submit">
        <van-button
          class="dl"
          round
          block
          color="#0085ff"
          type="info"
          @click="login"
          >登录</van-button
        >
        <van-button class="cj" round block color="##0989ff" plain hairline:true
          >创建账号</van-button
        >
      </div>
    </div>
    <div class="content">
      <div class="agress">
        登录即代表您同意
        <span>《用户服务协议》</span>
        <span>《隐私协议》</span>
      </div>
    </div>
  </div>
</template>

<script>
import { auLogin } from '@/api/login.js'
// 引入getLocal 保存token
import { setLocal, getLocal } from '@/utils/local.js'
export default {
  data () {
    return {
      form: {
        userName: 'hgkk2020', // 用户名
        password: 'abc123456' // 密码
      },
      rules: {
        userName: [
          {
            required: true,
            message: '请正确输入用户名',
            trigger: 'onChange'
          },
          {
            validater: value => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '请正确输入用户名',
            trigger: 'onChange'
          }
        ],
        passWord: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'onChange'
          }
        ]
      }
    }
  },
  created () {
    // 看是否有token
    if (getLocal('token')) {
      this.$router.push('/home/my')
    }
  },
  methods: {
    // 按钮返回发现页面
    toFind () {
      this.$router.push('home/find/find')
    },
    // 登录  思路：点击登录，表单验证，发送ajax请求
    async login () {
      this.form.password = this.$md5(this.form.password)
      this.$refs.form
        .validate()
        .then(() => {
          // alert('验证成功')
          // 登录
          auLogin(this.form).then(res => {
            window.console.log(res)
            // 保存token
            setLocal('token', res.data.data.token)
            setLocal('userId', res.data.data.userId)
            // 保存用户信息
            this.$store.commit('setUserInfo', res.data.data)
            // 设置登录状态   登录成功获取个人信息,保存到vuex中然后然后设置登录状态
            this.$store.commit('setIsLogin', true)
            // 渲染到我的页面
            this.$router.push('/home/my')
          })
        })
        .catch(error => {
          console.log('catch:', error)
        })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  position: relative;
  height: 100%;
  .iconfont {
    font-size: 30px;
  }
  .box {
    padding: 0 30px;
    h2 {
      font-size: 30px;
      margin-top: 30px;
      margin-bottom: 40px;
    }
    .input {
      .van-icon {
        color: #bcbcbc;
      }
      .user {
        display: flex;
        align-items: center;
        border-bottom: 1px solid #f7f7f7;
      }
      .mima {
        display: flex;
        align-items: center;
        margin-top: 10px;
        border-bottom: 1px solid #f7f7f7;
      }
    }
    .text {
      margin-top: 15px;
      font-size: 14px;
      line-height: 40px;
      color: #175199;
      display: flex;
      justify-content: space-between;
    }
    .submit {
      .dl {
        margin-top: 15px;
      }
      .cj {
        margin-top: 15px;
      }
    }
  }
  .content {
    position: absolute;
    bottom: 20px;
    left: 50px;
    .agress {
      text-align: center;
      font-size: 12px;
      span {
        color: #175199;
      }
    }
  }
}
</style>
